package com.acegear.horizon.domain.models.jpa;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.security.crypto.bcrypt.BCrypt;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * Created by wangsike on 2016/12/10.
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class PasswordAuth {

    @Column(unique = true)
    private Long userId;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;

    @Column(unique = true, columnDefinition = "VARCHAR(100)")
    private String username;

    @Column
    @JsonIgnore
    private String password;

    @CreatedDate
    @Column
    private Long createAt;

    @LastModifiedDate
    @Column
    private Long updateAt;

    public PasswordAuth() {

    }

    public PasswordAuth(Long userId, String username, String password) {
        this.userId = userId;
        this.username = username;
        this.password = BCrypt.hashpw(password, BCrypt.gensalt());
    }

    public Long getCreateAt() {
        return createAt;
    }

    public Long getUpdateAt() {
        return updateAt;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = BCrypt.hashpw(password, BCrypt.gensalt());
    }

    public boolean checkPassword(String password) {
        return this.password != null && BCrypt.checkpw(password, this.password);
    }
    
    public static void main(String[] args) {
    	System.out.println(BCrypt.checkpw("$2a$10$QrzBalI6EpvVSSsVi3Rqr.ddZOROIOZyC2TzAAr0Ftx5nXGehTu.W", "$2a$10$QrzBalI6EpvVSSsVi3Rqr.ddZOROIOZyC2TzAAr0Ftx5nXGehTu.W"));
	}
}
